﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon.S3;
using Amazon.S3.Model;

namespace Lincolnwood.Foundation.MediaStorage.AmazonS3
{
    public class AmazonS3MediaStorageProvider : IMediaStorageProvider
    {
        private string _bucketName;
        private string _accessKeyId;
        private string _secretKey;
        private string _basePath;
        private string _separatedBasePath;
        private const string PATHSEPARATOR = "/";
        private const string WEBURIPREFIX = "http://s3.amazonaws.com/";

        public AmazonS3MediaStorageProvider(string bucketName, string basePath)
        {
            _bucketName = bucketName;
            _separatedBasePath = basePath + PATHSEPARATOR;

        }

        public AmazonS3MediaStorageProvider(string accessKeyId, string secretKey, string bucketName, string basePath)
            : this(bucketName, basePath)
        {
            _accessKeyId = accessKeyId;
            _secretKey = secretKey;
        }

        public string GetWebAccessibleUri(string mediaUri)
        {
            return WEBURIPREFIX + _bucketName + PATHSEPARATOR + _separatedBasePath + mediaUri;
        }

        public List<MediaItem> GetMediaList()
        {
            List<MediaItem> mediaListResult = new List<MediaItem>();

            // Create a client
            AmazonS3Client client = new AmazonS3Client(_accessKeyId, _secretKey);

            // List all objects
            ListObjectsRequest listRequest = new ListObjectsRequest
            {
                BucketName = _bucketName,
            };

            ListObjectsResponse listResponse;
            do
            {
                // Get a list of objects
                listResponse = client.ListObjects(listRequest);
                foreach (S3Object obj in listResponse.S3Objects)
                {
                    // filter out the 'folder name'
                    if (obj.Key != _separatedBasePath)
                    {
                        var isolatedFileName = obj.Key.Substring(_separatedBasePath.Length, obj.Key.Length - _separatedBasePath.Length);

                        if (isolatedFileName != String.Empty) ;

                        MediaItem itemToAdd = new MediaItem(isolatedFileName, 
                            GetWebAccessibleUri(isolatedFileName), 
                            obj.Size, 
                            DateTime.Parse(obj.LastModified));

                        mediaListResult.Add(itemToAdd);
                    }
                    
                    //System.Diagnostics.Debug.WriteLine("Object - " + obj.Key);
                    //System.Diagnostics.Debug.WriteLine(" Size - " + obj.Size);
                    //System.Diagnostics.Debug.WriteLine(" LastModified - " + obj.LastModified);
                    //System.Diagnostics.Debug.WriteLine(" Storage class - " + obj.StorageClass);
                }

                // Set the marker property
                listRequest.Marker = listResponse.NextMarker;
            } while (listResponse.IsTruncated);

            return mediaListResult;
        }


    }
}
